<?php
class Category_RestController extends Ac_Rest_Controller
{

    /**
        list all category
    */
    public function indexAction() {
        $page = empty($_GET['page']) ? 1 : $_GET['page'];
        $per_page = 20;


        $params = array('status'=>1, 'path'=>array('$size' => 1));
        if (!empty($_GET['post_ad'])) {
            $params = array_merge($params, array('post_an_ad'=>1));
        } else {
            $params = array_merge($params, array('display_website'=>1));
        }
        $list = Model_Category::all($params)->sort(array('pos' => 1))->skip(($page-1) * $per_page)->limit($per_page);
        $tmp = array();
        foreach($list as $ele) {
            $tmp[] = $ele->exportRest();
        }
        $this->_helper->json($tmp);
    }

    /**

    list subcategories if exists
    list direct listing if exists
    */
    public function getAction() 
    {

        $compressed = false;
        if (!empty($_GET['co'])) {
            $compressed = true;
            unset($_GET['co']);
        }
        
        //var_dump($id   = $this->_request->getParams());exit;
        $id   = $this->_request->getParam('id');
        $res = array();

        $cat = Model_Category::one(array('_id' => new MongoId($id)));
        if (empty($cat)) {
            throw new Ac_Exception_MissingInformation('Category not found');
        }
           
        
        $res['info'] = $cat->exportRest();

        //find Children category
        $params = array('status' => 1, 'path' => array('$in' =>array(new MongoId($id)), '$size' => (int)count($cat->path)+1 ));
        if (!empty($_GET['post_ad'])) {
            $params = array_merge($params, array('post_an_ad'=>1));
        } else {
            $params = array_merge($params, array('display_website'=>array('$ne' => 0)));
        }
        $list = Model_Category::all($params)->sort(array('pos'=>1));

        $tmp = array();
        foreach($list as $ele) {
            $tmp[] = $ele->exportRest();
        }
        $res['children'] = $tmp;

        //find attached listing
        $limit = 20;
        if (!empty($_GET['ph'])) {
            $list = Model_Listing::all(array('phc'=> array('$gte' =>1), 'ca' => new MongoId($id), 'sa' => Model_Listing::STATUS_PUBLISHED))->sort(array('p_dt'=>-1));
        } else {
            $list = Model_Listing::all(array('ca' => new MongoId($id), 'sa' => Model_Listing::STATUS_PUBLISHED))->sort(array('p_dt'=>-1));
        }
        if (!empty($_GET['pg'])) {
            $minus = intval($_GET['pg']) > 0 ? intval($_GET['pg']) - 1 : 0;
            $list->skip($limit*$minus);
        }
        $list->limit($limit);

        $tmp2 = array();
        foreach($list as $ele) {
              $tmp = $ele->exportRest($compressed);
              if($compressed) {
                  $tmp['_simplified'] = true;
              }
              $tmp2[] = $tmp;              
        }
        $res['listing'] = $tmp2;
        unset($tmp2);
        //var_dump($res);exit;

        $this->_helper->json($res);
    }

    public function listingAction() 
    {
        $cat_id = $this->_request->get('id');

        $list = Model_Listing::all(array('ca' => new MongoId($cat_id)));

        $tmp = array();
        foreach($list as $ele) {
            $tmp[] = array(
                'title'       => $ele->title,
                'url'         => $ele->url(),
                'description' => $ele->description,
                'username'    => $ele->user->username,
            );
        }
        $this->_helper->json($tmp);
    }
}

